import React, { Fragment } from 'react';
import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField';
import { Grid, LinearProgress } from '@material-ui/core';
import PropTypes from 'prop-types';
import { withFormik, Field } from 'formik';
import Select from 'react-select';
import * as Yup from 'yup';
import { FormCheckbox, SingleSelect } from '../../components';
export const DisplayFormikState = props => (
<div style={{ margin: '1rem 0' }}>
<pre
style={{
background: '#f6f8fa',
fontSize: '.65rem',
padding: '.5rem',
}}
>
<strong>props</strong> ={' '}
{JSON.stringify(props, null, 2)}
</pre>
</div>
);
const formikEnhancer = withFormik({
enableReinitialize: true,
validationSchema: Yup.object().shape({
speciesId: Yup.string().required('Species is required'),
current: Yup.bool(),
tableId: Yup.string().required('Table is Required'),
noteId: Yup.string().required('Diet name is required'),
label: Yup.bool(),
dcId: Yup.string().required('Delivery Container is required'),
ncPrepares: Yup.bool(),
groupId: Yup.string().required('Group Diet is required, if no group diet. set to "NONE"'),
analyzed: Yup.bool(),
}),
mapPropsToValues: props => ({
speciesId: props.speciesId ? String(props.speciesId) : '',
current: props.current ? props.current === 1 : true,
tableId: props.tableId ? String(props.tableId) : '',
noteId: props.noteId ? props.noteId : '',
label: props.label ? props.label === 1 : true,
dcId: props.dcId ? String(props.dcId) : '',
ncPrepares: props.ncPrepares ? props.ncPrepares === 1 : true,
groupId: props.groupId ? String(props.groupId) : '1',
analyzed: props.analyzed === 1,
submitForm: props.submitForm,
}),
handleSubmit: (values, { setSubmitting }) => {
const {
current, label, ncPrepares, analyzed, submitForm, ...rest
} = values;
const payload = {
current: current ? 1 : 0,
label: label ? 1 : 0,
ncPrepares: ncPrepares ? 1 : 0,
analyzed: analyzed ? 1 : 0,
...rest,
};
submitForm(payload).then(() => {
setSubmitting(false);
}, () => {
setSubmitting(false);
});
},
displayName: 'MyForm',
});
const Form = props => {
const {
values: {
speciesId,
current,
tableId,
noteId,
label,
dcId,
ncPrepares,
groupId,
analyzed,
},
errors,
touched,
handleChange,
isValid,
setFieldTouched,
isSubmitting,
} = props;
const change = (name, e) => {
e.persist();
handleChange(e);
setFieldTouched(name, true, false);
};